﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BO;
using System.Data;
using AjaxControlToolkit;
using System.Globalization;
using System.Web.UI;
using System.Text;
using System.Reflection;


public class PaginaBase : System.Web.UI.Page
{
    public Usuario UsuarioLogado
    {
        get
        {
            if (Session["USUARIO"] != null)
                return (Usuario)Session["USUARIO"];
            else
                return null;
        }
        set { Session["USUARIO"] = value; }
    }

    public PaginaBase()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public static class ResponseHelper
    {
        public static void Redirect(string url, string target, string windowFeatures)
        {
            HttpContext context = HttpContext.Current;

            if ((String.IsNullOrEmpty(target) ||
                target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
                String.IsNullOrEmpty(windowFeatures))
            {

                context.Response.Redirect(url);
            }
            else
            {
                Page page = (Page)context.Handler;
                if (page == null)
                {
                    throw new InvalidOperationException(
                        "Cannot redirect to new window outside Page context.");
                }
                url = page.ResolveClientUrl(url);

                string script;
                if (!String.IsNullOrEmpty(windowFeatures))
                {
                    script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
                }
                else
                {
                    script = @"window.open(""{0}"", ""{1}"");";
                }

                script = String.Format(script, url, target, windowFeatures);
                ScriptManager.RegisterStartupScript(page,
                    typeof(Page),
                    "Redirect",
                    script,
                    true);
            }
        }
    }

    public static DateTime GetDate_doBanco()
    {
        ModeloContainer bco = new ModeloContainer();

        DateTime dtNow =  bco.ExecuteStoreQuery<DateTime>("SELECT NOW()").First();

        Parametro ehHorarioVerao = ParametroBO.Listar(0, "ehHorarioDeVerao").FirstOrDefault();
        if (ehHorarioVerao != null)
        {
            if (ehHorarioVerao.valor.ToUpper().Contains("S"))
                dtNow = dtNow.AddHours(-1);
        }

        return dtNow;
    }

    public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
    {
        DataTable dtReturn = new DataTable();


        // column names
        PropertyInfo[] oProps = null;


        if (varlist == null) return dtReturn;


        foreach (T rec in varlist)
        {
            // Use reflection to get property names, to create table, Only first time, others will follow
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();
                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;


                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }


                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }


            DataRow dr = dtReturn.NewRow();


            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
                (rec, null);
            }


            dtReturn.Rows.Add(dr);
        }
        return dtReturn;
    }
}